<?php
/**
 * @title 2641. 二叉树的堂兄弟节点 II
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return TreeNode
     */
    function replaceValueInTree($root) {
        $node = $root;

        /**
         * @since 2024-02-07 BFS广度遍历
         */
        $node->val = 0;
        $nodeArray = [$node];
        while($nodeArray){
            /**
             * @since 2024-02-07 下次遍历点的集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-02-07 遍历当前的节点集合
             */
            $sum = 0;
            foreach ($nodeArray as $node){
                /**
                 * @since 2024-02-07 下次遍历的点
                 */
                if($node->left !== null){
                    $sum += $node->left->val;
                    $nextNodeArray[] = $node->left;
                } else {}
                if($node->right !== null) {
                    $sum += $node->right->val;
                    $nextNodeArray[] = $node->right;
                } else {}
            }

            /**
             * @since 2024-02-07 重新赋值
             */
            foreach ($nodeArray as $node){
                if($node->left !== null AND $node->right !== null){
                    $node->left->val = $node->right->val = $sum - $node->left->val - $node->right->val;
                } else {
                    if($node->left !== null){
                        $node->left->val = $sum - $node->left->val;
                    } else {}
                    if($node->right !== null){
                        $node->right->val = $sum - $node->right->val;
                    } else {}
                }
            }

            /**
             * @since 2024-02-07 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return TreeNode
         */
        return $root;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202402/2024.02.07.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
